import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
const pathSrc = resolve(__dirname, 'src/renderer/src')
import { name, version, dependencies, devDependencies } from './package.json'

// 平台的名称、版本、运行所需的 node 版本、依赖、构建时间的类型提示
const __APP_INFO__ = {
  pkg: { name, version, dependencies, devDependencies },
  buildTimestamp: Date.now()
}

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()],
    resolve: {
      alias: {
        '@main': resolve(__dirname, 'src/main')
      }
    }
  },
  preload: {
    plugins: [externalizeDepsPlugin()],
    resolve: {
      alias: {
        '@preload': resolve(__dirname, 'src/preload')
      }
    }
  },
  renderer: {
    resolve: {
      alias: {
        '@renderer': resolve('src/renderer/src'),
        '@': resolve('src/renderer/src')
      }
    },
    css: {
      preprocessorOptions: {
        // 定义全局 SCSS 变量
        scss: {
          api: 'modern-compiler',
          additionalData: `@use '@renderer/assets/styles/variables.scss' as *;`
        }
      }
    },
    plugins: [
      vue(),
      // API 自动导入
      AutoImport({
        // 导入 Vue 函数，如：ref, reactive, toRef 等
        imports: ['vue', 'pinia', 'vue-router', '@vueuse/core', 'vue-i18n'],
        resolvers: [ElementPlusResolver({ importStyle: 'sass' })], //注意需要设置importStyle否则无法覆盖全局的样式
        eslintrc: {
          enabled: false,
          filepath: './.eslintrc-auto-import.json',
          globalsPropValue: true
        },
        vueTemplate: true,
        dts: resolve(pathSrc, 'types/auto-imports.d.ts') //导入函数自动声明文件 false为关闭
        // dts: false
      }),
      // 组件自动导入
      Components({
        resolvers: [
          ElementPlusResolver({ importStyle: 'sass' }) // 自动注册图标组件
        ],
        dts: resolve(pathSrc, 'types/components.d.ts'), //导入组件类型声明文件路径 (false:关闭自动生成)
        dirs: resolve(pathSrc, 'components') // ['src/renderer/src/components'] // 指定自定义组件位置(默认:src/components)
      })
    ],
    // 关键：多页面配置（主窗口 + 画中画窗口）
    build: {
      rollupOptions: {
        input: {
          // 主窗口入口
          main: resolve('src/renderer/index.html'),
          // 画中画窗口入口（新增）
          pip: resolve('src/renderer/pip.html')
        }
      }
    },
    define: {
      __APP_INFO__: JSON.stringify(__APP_INFO__)
    }
  }
})
